package com.study.springsecurity.security.handler;

import com.study.springsecurity.constant.ErrorCode;
import com.study.springsecurity.pojo.Result;
import com.study.springsecurity.utils.ResponseUtil;
import com.study.springsecurity.utils.SecurityContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 登录认证成功后处理
 *
 * @Author YL
 * @Create 2024/4/4 1:17
 * @Version 1.0
 */
@Slf4j
public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        log.info("MyAuthenticationSuccessHandler onAuthenticationSuccess login success,登录成功，用户：{}", SecurityContextUtil.getUserName());
        Result result = new Result();
        result.setCode(ErrorCode.E000000.code()).setMessage(ErrorCode.E000000.msg());
        ResponseUtil.writeJsonUtf8(response, HttpStatus.OK, result);
    }
}
